package dao

import (
	"github.com/jinzhu/gorm"
	"pojo/model"
)

//购物车管理
type ShoppingDao struct {
	db *gorm.DB
}

// 构造一个对象传入
func NewShoppingDao(db *gorm.DB) *ShoppingDao {
	return &ShoppingDao{
		db: db,
	}
}

/**
添加到购物车
 */
 func (this *ShoppingDao)InsertShopping(shopping model.Shopping)(int64,error){
	 create := this.db.Create(shopping)
	 return  create.RowsAffected,create.Error
 }

 /**
 修改购物车
  */
func (this *ShoppingDao)UpdataShopping(shopping model.Shopping)(int64,error) {
	update := this.db.Update(shopping)
	return update.RowsAffected,update.Error
}

/**
删除购物车
 */
func  (this *ShoppingDao)DeleteShopping(id int64)(int64,error)  {
	db := this.db.Delete(model.Shopping{}, id)
	return db.RowsAffected,db.Error
}

/**
查询购物车
 */
func (this *ShoppingDao)ListShopping(page,limit int32)([]*model.Shopping,error)  {
	shoppings := []*model.Shopping{}
	find := this.db.Limit(limit).Offset((page - 1) * limit).Find(&shoppings)
	return shoppings,find.Error
}